import * as path from 'node:path'
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { JwtModule } from '@nestjs/jwt'
import { LoginGuard } from './guard/login.guard'
import { PermissionGuard } from './guard/permission.guard'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { User } from './modules/user/entities/user.entity'
import { History } from './modules/user/entities/history.entity'
import { Role } from './modules/role/entities/role.entity'
import { Menu } from './modules/menu/entities/menu.entity'
import { Permission } from './modules/permission/entities/permission.entity'
import { Meeting } from './modules/meeting/entities/meeting.entity'
import { RedisModule } from './redis/redis.module'
import { EmailModule } from './email/email.module'
import { UserModule } from './modules/user/user.module'
import { RoleModule } from './modules/role/role.module'
import { MenuModule } from './modules/menu/menu.module'
import { PermissionModule } from './modules/permission/permission.module'
import { MeetingModule } from './modules/meeting/meeting.module'
import { AuthInterceptor } from './interceptor/auth.interceptor'

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory(configService: ConfigService) {
        return {
          type: 'mysql',
          host: configService.get('mysql_server_host'),
          port: configService.get('mysql_server_port'),
          username: configService.get('mysql_server_username'),
          password: configService.get('mysql_server_password'),
          database: configService.get('mysql_server_database'),
          synchronize: false, // 线上环境关闭
          logging: true,
          entities: [
            User,
            Role,
            Permission,
            Meeting,
            Menu,
            History,
          ],
          poolSize: 10,
          timezone: 'Asia/Shanghai',
          connectorPackage: 'mysql2',
          extra: {
            authPlugin: 'sha256_password',
          },
        }
      },
      inject: [ConfigService],
    }),
    JwtModule.registerAsync({
      global: true,
      useFactory(configService: ConfigService) {
        return {
          secret: configService.get('jwt_secret'),
          signOptions: {
            expiresIn: configService.get('jwt_access_token_expires_time'),
          },
        }
      },
      inject: [ConfigService],
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      // 如果在多个文件中找到一个变量，则第一个优先。
      envFilePath: [path.join(__dirname, '.env')],
    }),
    RedisModule,
    EmailModule,
    UserModule,
    RoleModule,
    MenuModule,
    PermissionModule,
    MeetingModule,
  ],
  controllers: [AppController],
  // 需要依赖注入
  providers: [
    AppService,
    {
      provide: 'APP_GUARD',
      useClass: LoginGuard,
    },
    {
      provide: 'APP_GUARD',
      useClass: PermissionGuard,
    },
    {
      provide: 'APP_INTERCEPTOR',
      useClass: AuthInterceptor,
    },
  ],
})
export class AppModule { }
